package wat.ziszcommon.clientgui;
import java.io.BufferedReader;
import java.io.FileNotFoundException;
import java.io.FileReader;
import java.io.IOException;
import java.io.Writer;
import java.lang.reflect.InvocationTargetException;
import java.util.ArrayList;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.swing.JTree;
import javax.swing.ToolTipManager;
import javax.swing.tree.DefaultMutableTreeNode;
import javax.swing.tree.DefaultTreeModel;
import javax.swing.tree.TreePath;

/**
 *
 * @author kubaolszewski
 */

public class MyJTree extends JTree{
    private DefaultMutableTreeNode rootNode;
    private DefaultTreeModel treeModel;
    private ArrayList FolderList = new ArrayList();
    
    public MyJTree(DefaultTreeModel treeModel, DefaultMutableTreeNode rootNode){
        super(treeModel);
        this.rootNode = rootNode;
        this.treeModel = treeModel;
    }
    
    public DefaultMutableTreeNode addObject(Object child) {
        DefaultMutableTreeNode parentNode = null;
        TreePath parentPath = this.getSelectionPath();

        if (parentPath == null) {
            parentNode = rootNode;
        } else {
            parentNode = (DefaultMutableTreeNode)
                         (parentPath.getLastPathComponent());
        }

        return addObject(parentNode, child, true);
    }

    public DefaultMutableTreeNode addObject(DefaultMutableTreeNode parent,
                                            Object child) {
        return addObject(parent, child, false);
    }

    public DefaultMutableTreeNode addObject(DefaultMutableTreeNode parent,
                                            Object child, 
                                            boolean shouldBeVisible) {
        DefaultMutableTreeNode childNode = 
                new DefaultMutableTreeNode(child);

        if (parent == null) {
            parent = rootNode;
        }
	
	//It is key to invoke this on the TreeModel, and NOT DefaultMutableTreeNode
        treeModel.insertNodeInto(childNode, parent, 
                                 parent.getChildCount());

        //Make sure the user can see the lovely new node.
        if (shouldBeVisible) {
            this.scrollPathToVisible(new TreePath(childNode.getPath()));
        }
        return childNode;
    }
    public void createTree(String path){
        try {
            this.createNewTree(path);
            this.repaintTree();
        } catch (NoSuchMethodException ex) {
            Logger.getLogger(MyJTree.class.getName()).log(Level.SEVERE, null, ex);
        } catch (FileNotFoundException ex) {
            Logger.getLogger(MyJTree.class.getName()).log(Level.SEVERE, null, ex);
        }
        
    }
    
    public void repaintTree(){
        MyTreeCellRenderer tcr = new MyTreeCellRenderer();
        this.setCellRenderer(tcr);
        ToolTipManager.sharedInstance().registerComponent(this); 
    }
    
    public void createNewTree(String path) throws FileNotFoundException, NoSuchMethodException{
        BufferedReader inputStream = null;
        try {
            inputStream = new BufferedReader(new FileReader(path));
            //inputStream = new BufferedReader(new FileReader("C:\\test.txt"));
            String l;
            String sciezka;
            String klasa;
            String nazwa;
            int nr,nr2;
            while ((l = inputStream.readLine()) != null) {
                nr=l.indexOf(";", 0);
                System.out.println("numer "+nr);
                if (nr!=-1){
                    sciezka=l.substring(0, nr);
                    //nr2=l.indexOf(nr+1, 0);
                    nr2=l.indexOf(";", nr+1);
                    //System.out.println("numer2 "+nr2);
                    klasa=l.substring(nr+1,nr2);
                    nazwa=l.substring(nr2+1);
                    //System.out.println("path "+sciezka);
                    //System.out.println("class "+klasa);
                    //System.out.println("name "+nazwa);
                    MyJPanel obj = (MyJPanel) Class.forName(klasa).
                    getConstructor(new Class[]{String.class, MyJTree.class}).newInstance(nazwa,this);
                    this.addObject(creatMyPath(sciezka).getTreeFolder(),obj);
                }
            }
        } catch (InstantiationException ex) {
            Logger.getLogger(MyJTree.class.getName()).log(Level.SEVERE, null, ex);
        } catch (IllegalAccessException ex) {
            Logger.getLogger(MyJTree.class.getName()).log(Level.SEVERE, null, ex);
        } catch (IllegalArgumentException ex) {
            Logger.getLogger(MyJTree.class.getName()).log(Level.SEVERE, null, ex);
        } catch (InvocationTargetException ex) {
            Logger.getLogger(MyJTree.class.getName()).log(Level.SEVERE, null, ex);
        } catch (ClassNotFoundException ex) {
            Logger.getLogger(MyJTree.class.getName()).log(Level.SEVERE, null, ex);
        }catch (IOException ex) {
            Logger.getLogger(MyJTree.class.getName()).log(Level.SEVERE, null, ex);
        }        
    }
    
    private MyFolder creatMyPath(String sciezka){
        int k = 0;
        int p = 0;
        String katalog = null;
        String rodzic = null;
        String myPath="";
        MyFolder folder = null;
        k=sciezka.indexOf("/",p);
        while(k !=-1){
            katalog=sciezka.substring(p,k);
            p=k+1;
            k=sciezka.indexOf("/",p); 
            if(rodzic==null){
                myPath="";
            }else{
                myPath=myPath+rodzic+"/";
            }
            if(this.getFolderByPath(myPath+katalog+"/")==-1){
              folder = createFolder(katalog,rodzic,myPath);
            }
            //System.out.println("katalog: "+katalog);
            //System.out.println("sciezka: "+myPath);
            rodzic=katalog;
        }
        //return (MyFolder) FolderList.get(this.getFolder(katalog));
        return (MyFolder) FolderList.get(this.getFolderByPath(myPath+katalog+"/"));
     }
    
    private int getFolder(String nowy){
        int i;
        for(i=0; i<FolderList.size(); i++){
            if(((MyFolder)FolderList.get(i)).getNazwa().equals(nowy)) return i;              
        }
        return -1;
    }
    
    private int getFolderByPath(String path){
        int i;
        for(i=0; i<FolderList.size(); i++){
            if(((MyFolder)FolderList.get(i)).getFullPath().equals(path)) return i;              
        }
        return -1;
    }
    
    private MyFolder createFolder(String nowy, String rodzic, String myPath){
        int i;
        MyFolder newFolder=null;
        if(rodzic==null){
            newFolder = new MyFolder(null,nowy,this);
        }else{
            //i=this.getFolder(rodzic);
            i=this.getFolderByPath(myPath);
            if(i==-1){
                newFolder = new MyFolder(null,nowy,this);
            }else{
                newFolder = new MyFolder((MyFolder)FolderList.get(i),nowy,this);
            }
        }
        newFolder.setMyPath(myPath);
        FolderList.add(newFolder);
        return newFolder;
    }
    
}
